package com.vteApp;

import java.util.List;

import com.vteApp.bl.Risk;

import android.widget.TableLayout;

public abstract class RiskActivity extends VTEAppActivity {

	List<RiskRow> riskRows;
	TableLayout tableLayout;

	@Override
	protected void onStart() {
		super.onStart();
		setContentView(R.layout.risklayout);
		tableLayout = (TableLayout) findViewById(R.id.tableLayout1);
		riskRows = getRiskRows();
		addRowsToTable(riskRows);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onStop() {
		super.onStop();
		saveState();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	public abstract List<RiskRow> getRiskRows();

	private void addRowsToTable(List<RiskRow> rows) {
		tableLayout.removeAllViews();
		for (RiskRow riskRow : rows) {
			TableLayout tbLayout = (TableLayout) riskRow.getParent();
			if (tbLayout != null)
				tbLayout.removeView(riskRow);
			tableLayout.addView(riskRow);
		}

	}

	private void saveState() {
		for (RiskRow riskRow : riskRows) {
			Risk risk = riskRow.getRisk();
			risk.setValue(riskRow.getButton().isChecked());
		}
	}

}
